package com.android.bookgarden.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.bookgarden.R;
import com.android.bookgarden.adapter.LeanBookAdapter;
import com.android.bookgarden.apis.Apis;
import com.android.bookgarden.base.BaseActivity;
import com.android.bookgarden.bean.DataBean;
import com.android.bookgarden.bean.HomeDataBean;
import com.android.bookgarden.bean.LeanBookBean;
import com.android.bookgarden.bean.SmsMode;
import com.android.bookgarden.constract.RealtConstract;
import com.android.bookgarden.network.HttpType;
import com.android.bookgarden.persenter.BasePersenter;
import com.android.bookgarden.utli.JsonUitl;
import com.android.bookgarden.utli.LogUtil;
import com.android.bookgarden.utli.Utlis;
import com.android.bookgarden.views.LoadingDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Create by zouqh
 * on 2018/11/30
 **/
public class LeanBookAcitity extends BaseActivity implements View.OnClickListener, RealtConstract.View {
    @BindView(R.id.close)
    ImageView close;
    @BindView(R.id.title_text)
    TextView titleText;
    @BindView(R.id.ritgh_icon)
    ImageView ritghIcon;
    @BindView(R.id.ritgh_text)
    TextView ritghText;
    @BindView(R.id.recycler)
    RecyclerView recycler;
    private int pageIndex = 1;
    private int pageSize = 20;
    private boolean flag = false;
    private List<LeanBookBean> list;
    private LeanBookAdapter adapter;
    private RealtConstract.Presenter presenter;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_lean_book;
    }

    @Override
    protected void initView() {
        presenter = new BasePersenter(this,this);
        close.setOnClickListener(this);
        titleText.setText("已学的课程");
        list= new ArrayList<>();
//        list.add(new LeanBookBean("毕节市最好的指尖芭蕾少儿舞蹈培训学校",""));
//        list.add(new LeanBookBean("毕节市最好的指尖芭蕾少儿舞蹈培训学校",""));
//        list.add(new LeanBookBean("毕节市最好的指尖芭蕾少儿舞蹈培训学校",""));
//        list.add(new LeanBookBean("毕节市最好的指尖芭蕾少儿舞蹈培训学校",""));
//        list.add(new LeanBookBean("毕节市最好的指尖芭蕾少儿舞蹈培训学校",""));
//        list.add(new LeanBookBean("毕节市最好的指尖芭蕾少儿舞蹈培训学校",""));
//        list.add(new LeanBookBean("毕节市最好的指尖芭蕾少儿舞蹈培训学校",""));
        adapter = new LeanBookAdapter(list,this);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recycler.setLayoutManager(manager);
        recycler.setHasFixedSize(true);
        recycler.setAdapter(adapter);
        adapter.addFooterView(R.layout.layout_footer);
        getDataList();
    }

    private void getDataList() {
        LoadingDialog.showMassge(this,"加载中...");
        presenter.loadDataHeader(null, "", Apis.LEAR_BOOK + pageIndex + "/" + pageSize , 1001, Utlis.getHeadler(this), HttpType.GET);
    }

    @Override
    public void onRequestPermissions(String str, boolean z) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.close:
                finish();
                break;
        }
    }


    @Override
    public void returnData(String data, int urlCode) {
        LogUtil.e("返回数据", data + "");
        if (TextUtils.isEmpty(data)) {
            return;
        }
        LoadingDialog.hideProgress();
        handler.obtainMessage(urlCode, data).sendToTarget();
    }

    @Override
    public void LoadError(String errorMsg, int urlCode) {
        showToast("请求失败");
        LoadingDialog.hideProgress();
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            String data = (String) msg.obj;
            switch (msg.what) {


                case 1001:
//                    refreshLayout.setRefreshing(false);
                    DataBean bean = Utlis.getDataBean(data);
                    if (null == bean) {
                        showToast("获取列表失败");
                        return;
                    }
                    if (bean.getCode() == 200) {
                        if (bean.getCode() == 200) {
                            List<LeanBookBean> listD = JsonUitl.stringToList(bean.getData(), LeanBookBean.class);
                            if (null == listD || listD.size() <= 0) {
                                adapter.setFooterVisible(View.GONE);
                                flag = false;
                                return;
                            }
                            list.addAll(listD);
                            adapter.notifyDataSetChanged();
                            if (listD.size()<pageSize){
                                adapter.setFooterVisible(View.GONE);
                                flag = false;
                            }else {
                                flag = true;
                            }

                        } else {
                            showToast(bean.getMessage() + "");
                        }


                        break;

                    } else {
                        showToast(bean.getMessage() + "");
                    }


                    break;



            }
        }
    };
}
